<template>
    <el-form :model="form" :rules="rules" ref="registerForm" label-width="80px" class="register-form">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password" />
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-select v-model="form.role" placeholder="请选择角色">
          <el-option label="学生" value="STUDENT" />
          <el-option label="讲师" value="INSTRUCTOR" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">注册</el-button>
      </el-form-item>
    </el-form>
  </template>
  
  <script>
  import request from '@/utils/request'
  
  export default {
    name: 'Register',
    data() {
      return {
        form: {
          username: '',
          email: '',
          password: '',
          role: ''
        },
        rules: {
          username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
          email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
          password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
          role: [{ required: true, message: '请选择角色', trigger: 'change' }]
        }
      }
    },
    methods: {
      onSubmit() {
        this.$refs.registerForm.validate(valid => {
          if (!valid) return;
          request.post('/api/users/register', this.form)
            .then(() => {
              this.$message.success('注册成功！');
              this.$router.push('/login');
            })
            .catch(err => {
              // 错误提示已在axios拦截器中处理
            });
        });
      }
    }
  }
  </script>
  
  <style scoped>
  .register-form {
    max-width: 400px;
    margin: 60px auto;
    padding: 30px 20px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px #eee;
  }
  </style>


